﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;
using System.Data;

namespace WebSite.admin.member
{
    public partial class member : BaseAdmin
    {
        readonly DALMember theMember = new DALMember();
        tbMember oMember;
        readonly MemberAccount theMemberAccount = new MemberAccount();
        // Methods
        private void DeletedMember()
        {

            if ((base.Request.QueryString["mid"] != null) && (base.Request.QueryString["action"] != null))
            {
                oMember = theMember.GetModel(int.Parse(base.Request.QueryString["mid"].ToString()));
                oMember.flag = 2;
                theMember.Update(oMember);
                ExtAspNet.Alert.Show("删除会员成功！");
                this.BindGrid();
            }
        }




    

        protected void Page_Load(object sender, EventArgs e)
        {
   
        
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.UserList);
                this.DeletedMember();
                this.BindGrid();
             
            }
        }


        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        string GetSqlWhere
        {
            get
            {

                StringBuilder strWhere = new StringBuilder();

                if (this.txtLoginName.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and memberlogin like '%{0}%'", this.txtLoginName.Text.Trim());
                }
                if (this.txtTelPhone.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and tel1 like '%{0}%'", this.txtTelPhone.Text.Trim());
                }
                if (this.txtCardCode.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and  memberid in (select memberid from tbMemberAccount MA join DB_Card C on MA.Cardid = C.CardID and C.CardCode like like '%{0}%')", this.txtCardCode.Text.Trim());
                }
                return strWhere.ToString();
            }
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table =this.theMember.GetListByPage(GetSqlWhere, string.Format("{0} {1}", e.SortField, e.SortDirection), Grid1.PageIndex, Grid1.PageSize).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            ViewState["sort"] = e.SortField;
            ViewState["dir"] = e.SortDirection;
        }
        private void BindGrid()
        {   // 1.设置总项数
            Grid1.RecordCount = theMember.GetRecordCount(GetSqlWhere);
            string strOrderBy = string.Format("{0} {1} "
                                               , ViewState["sort"] == null ? null : ViewState["sort"].ToString()
                                               , ViewState["dir"] == null ? null : ViewState["dir"].ToString());
            if (strOrderBy.Length < 13)
            {
                strOrderBy = "registerTime desc ";
            }
            DataTable table = theMember.GetListByPage(GetSqlWhere, strOrderBy, Grid1.PageIndex, Grid1.PageSize).Tables[0];

            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };

        }

        protected void Grid1_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
            DataSet ds;
            System.Text.StringBuilder sbContent = new System.Text.StringBuilder();

            ds = theMemberAccount.GetListByMemberId(int.Parse(this.Grid1.DataKeys[e.RowIndex][0].ToString()));
            if (ds != null && ds.Tables.Count > 0)
            {
                int iCount = 0;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {

                    sbContent.Append(dr["cardcode"].ToString() + "|");
                    iCount++;
                    if (iCount % 5 == 0)
                    {
                        sbContent.Append("\n");
                    }
                }
            }
            e.Values[5] = sbContent.ToString();

        }


        protected void btnBrachDeleted_Click1(object sender, EventArgs e)
        {
            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sbProIds = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
                if (i == selectedCount - 1)
                {
                    sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
                }
                else
                {
                    sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
                }
            }
            if (sbProIds.Length > 0)
            {
               this.theMember.DeleteList(sbProIds.ToString());
            }
            ExtAspNet.Alert.Show("批量删除会员成功！");
            this.BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

    }
}